//
//  LivingViewController.m
//  FKGW
//
//  Created by song ce on 2017/3/1.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "LivingViewController.h"
#import "LivingCell.h"
#import "SelecedViewController.h"
#import "AppDelegate.h"
#import "liveCenterViewController.h"
#import "YBPopupMenu.h"
#import "LiveFooterViewController.h"
#import "ProductModel.h"

#define TITLES @[@"全部", @"裕华区", @"新华区",@"桥西区",@"长安区"]
@interface LivingViewController ()<YBPopupMenuDelegate,UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableV;
@property(nonatomic,strong)NSMutableArray *dataSource;

@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIButton *zoneButton;
@property (weak, nonatomic) IBOutlet UIButton *indurstyButton;
@property (weak, nonatomic) IBOutlet UIButton *moodsButton;
@property (weak, nonatomic) IBOutlet UIButton *attionButtion;
@property(nonatomic,strong)YBPopupMenu *popupMenu;
@property(nonatomic,strong)NSMutableArray *zoneArray;//区域
@property(nonatomic,strong)NSMutableArray *industryArray;//行业
@property(nonatomic,strong)NSMutableArray *peopleArray;//人气
@property(nonatomic,strong)NSMutableArray *attentionArray;//关注
@property(nonatomic,strong)NSMutableArray *selectedArray;//
@property(nonatomic,assign)NSInteger   num;//区分以上四种条件
//@property(nonatomic,strong)NSMutableArray *butArr;
@property(nonatomic,strong)UIButton *selectedButton;//记录当前选择的按钮
@property(nonatomic,copy)NSString *ACCode;
@property(nonatomic,copy)NSString *industryId;
@property(nonatomic,copy)NSString *popularitySort;
@property(nonatomic,copy)NSString *attentionSory;
@end

@implementation LivingViewController

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [_popupMenu dismiss];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.ACCode=@"";
    self.industryId = @"";
    self.popularitySort = @"1";
    self.attentionSory = @"1";
    self.topView.backgroundColor = [UIColor blackColor];
    self.dataSource = [NSMutableArray array];
    self.zoneArray = [NSMutableArray array];
    self.industryArray = [NSMutableArray array];
    self.peopleArray = [NSMutableArray array];
    self.attentionArray = [NSMutableArray array];
    [self tableViewAndHeaderView];
    [self backButton];
    [self customtitV];
    [self rightButton];
    //加载直播界面行业列表
    [self loadIndustryList];
    //加载数据
    [self loadData];
    
}
//2.70直播店铺列表
-(void)loadData{
    NSDictionary *paramDic =
                              @{@"ACCode":self.ACCode,
                                @"industryId":self.industryId,
                                @"popularitySort":self.popularitySort,
                                @"attentionSory":self.attentionSory
                               };
    __weak typeof(self)  weakSelf = self;
    [MBProgressHUD showLoadingGif];
    [Tools AFNetworkingRequestTypePostWithUrl:GETLIVESTORE paramters:paramDic afterResultBlock:^(NSDictionary *resultDic) {
         if([resultDic[@"flag"] isEqualToString:@"true"]){
             NSArray *data = resultDic[@"data"];
             if([data isKindOfClass:[NSNull class]]){
                 [MBProgressHUD dismissHUD];
                 return ;
             }
             if(weakSelf.dataSource.count){
                 [weakSelf.dataSource removeAllObjects];
             }
             for(NSDictionary *dic in data){
                 LiveStoreModel *model = [LiveStoreModel modelWithDic:dic];
                 [weakSelf.dataSource addObject:model];
             }
             [weakSelf.tableV reloadData];
             weakSelf.tableV.hidden = NO;
             [MBProgressHUD dismissHUD];
         }
    }];
}

//2.71直播界面行业列表
-(void)loadIndustryList{
    __weak typeof(self) weakSelf = self;
    [Tools AFNetworkingRequestTypePostWithUrl:INDUSTRYLIST paramters:nil afterResultBlock:^(NSDictionary *resultDic) {
        if([resultDic[@"flag"] isEqualToString:@"true"]){
            NSArray *data = resultDic[@"data"];
            if([data isKindOfClass:[NSNull class]])return ;
            for(NSDictionary *dic in data){
                ProductModel *pmodel = [[ProductModel alloc]init];
                pmodel.name = dic[@"name"];
                pmodel.id = dic[@"industryId"];
                [weakSelf.industryArray addObject:pmodel];
            }
        }
    }];
}

- (void)backButton{
    UIImage *image = SetImage(@"nav_icon_back_nor");
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(12, 18, 25, 40)];
    [btn addTarget:self action:@selector(backBtn) forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:image forState:UIControlStateNormal];
    [self.naviBar addSubview:btn];
}

-(void)backBtn{
     SelecedViewController *selectedViewConteroller = [[SelecedViewController alloc]init];
    BaseNavigationViewController *navi = [[BaseNavigationViewController alloc]initWithRootViewController:selectedViewConteroller];
    [[AppDelegate appDealegate].window setRootViewController:navi];
}
- (void)customtitV{
    UIButton *searchButton= [UIButton buttonWithType:UIButtonTypeCustom];
    searchButton.frame = CGRectMake(0, 0, 60 , 40);
    [searchButton setImage:[UIImage imageNamed:@"nav_icon_diquxiala_nor"] forState:UIControlStateNormal];
    [searchButton setTitle:@"石家庄" forState:UIControlStateNormal];
    searchButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [searchButton setImageEdgeInsets:UIEdgeInsetsMake(0, 48, 0, 0)];
    [searchButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -18, 0, 0)];
    [searchButton setTitleColor:DEF_COLOR  forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(locationAction) forControlEvents:UIControlEventTouchUpInside];
    searchButton.centerX = Main_Screen_Width/2;
    searchButton.centerY = self.naviBar.centerY+8;
    [self.naviBar addSubview:searchButton];

}
//跳转到位置页面
- (void)locationAction{
    
    DLog(@"shijiaz");
    
}

-(void)rightButton{
    self.isNeedMessageItem = YES;
    
    UIButton *locationButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
    locationButton2.frame=CGRectMake(0, 0, 40, 40);
    [locationButton2 setTitle:@"足迹" forState:UIControlStateNormal];
    [locationButton2 setTitleColor:DEF_COLOR  forState:UIControlStateNormal];
    locationButton2.titleLabel.font = [UIFont systemFontOfSize:15];
    [locationButton2 addTarget:self action:@selector(right2_Action) forControlEvents:UIControlEventTouchUpInside];
    self.rightItem2 =locationButton2;
}

-(void)right1Action{
    
    
    
}

-(void)right2_Action{
    if(![AppDelegate appDealegate].uid.length){
        LoginViewController *login = [[LoginViewController alloc]init];
        login.isModel = YES;
        BaseNavigationViewController *baseNavi = [[BaseNavigationViewController alloc]initWithRootViewController:login];
        [self presentViewController:baseNavi animated:YES completion:nil];
        return;
    }
    LiveFooterViewController *liveFooter = [[LiveFooterViewController alloc]init];
    liveFooter.vcName = @"足迹";
    [self.navigationController pushViewController:liveFooter animated:YES];
}

- (void)tableViewAndHeaderView{
    //表视图
    _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, kNaviBarHeight+31, Main_Screen_Width, Main_Screen_Height-kNaviBarHeight-31) style:UITableViewStyleGrouped];
    _tableV.showsVerticalScrollIndicator = NO;
    _tableV.backgroundColor = [UIColor colorWithRed:242./255 green:242./255 blue:242./255 alpha:1.];
    _tableV.delegate = self;
    _tableV.dataSource =self;
    [self.view addSubview:_tableV];
    _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableV.tableFooterView = [[UIView alloc]init];
    _tableV.hidden = YES;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;    {
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //    return self.dataSource.count;
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *ident=@"actell";
    LivingCell *cell=[tableView dequeueReusableCellWithIdentifier:ident];
    if(cell==nil){
        cell=[[[NSBundle mainBundle]loadNibNamed:@"LivingCell" owner:self options:nil]lastObject];
    }
    cell.clickBlock = ^{
        liveCenterViewController *liveCenter = [[liveCenterViewController alloc]init];
//        [self presentViewController:liveCenter animated:YES completion:nil];
        [self.navigationController pushViewController:liveCenter animated:YES];
    };
    //       cell.model = self.dataSource[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 311;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
 }
- (IBAction)topButton:(UIButton *)sender {
    [self showOrHiddenYBPopMenu:sender titles:TITLES];
}

- (void)showOrHiddenYBPopMenu:(UIButton *)sender titles:(NSArray*)titles {
    if(self.selectedButton!=nil&&self.selectedButton!=sender&&_popupMenu!=nil){
        [self popMenuToNil:self.selectedButton];
        return;
    }
    sender.selected = !sender.selected;
    if(sender.isSelected){
        self.selectedButton = sender;
        self.num= sender.tag;
        if(self.num ==0){
            self.selectedArray = self.industryArray;
        }else if(self.num ==1){
           self.selectedArray = self.industryArray;
        }else if(self.num ==2){
             self.selectedArray = self.industryArray;
        }else{
             self.selectedArray = self.industryArray;
        }
        _popupMenu = [YBPopupMenu  showAtPoint:CGPointMake(sender.left, self.topView.bottom) titles:self.selectedArray icons:nil menuWidth:sender.width  view:self.view  delegate:self];
        _popupMenu.backGroudColor = [UIColor blackColor];
        _popupMenu.text_Color = [UIColor whiteColor];
        [sender setImage:[UIImage imageNamed:@"icon_fenleixiala_nor"] forState:UIControlStateSelected];
           [sender setTitleColor:DEF_COLOR forState:UIControlStateNormal];
    }else{
        [_popupMenu dismiss];
        _popupMenu = nil;
        [sender setImage:[UIImage imageNamed:@"icon_hangyexiala_nor"] forState:UIControlStateNormal];
            [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
}

- (void)popMenuToNil:(UIButton *)sender {
    sender.selected = !sender.selected;
    if(!sender.isSelected){
        [_popupMenu dismiss];
             _popupMenu = nil;
        [sender setImage:[UIImage imageNamed:@"icon_hangyexiala_nor"] forState:UIControlStateNormal];
            [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
  }
#pragma mark - YBPopupMenuDelegate
- (void)ybPopupMenuDidSelectedAtIndex:(NSInteger)index ybPopupMenu:(YBPopupMenu *)ybPopupMenu
{
    ProductModel *model = self.selectedArray[index];
    if(self.num==0){
        [self popMenuToNil:self.zoneButton];
        self.ACCode = @"";
    }else if(self.num==1){
        [self popMenuToNil:self.indurstyButton];
        self.industryId = model.id;
    }else if(self.num==2){
        [self popMenuToNil:self.moodsButton];
        self.popularitySort = @"1";
    }else if(self.num==3){
        [self popMenuToNil:self.attionButtion];
        self.attentionSory = @"1";
    }
     [self.attionButtion setTitle:model.name  forState:UIControlStateNormal];
    [self loadData];
    DLog(@"点击了 %@ 选项",model.name);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
